/*
 * Copyright (c) 2023 OceanBase.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.oceanbase.odc.metadb.notification;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * @author liuyizhuo.lyz
 * @date 2024/1/17
 */
public interface MessageSendingHistoryRepository extends JpaRepository<MessageSendingHistoryEntity, Long>,
        JpaSpecificationExecutor<MessageSendingHistoryEntity> {

    @Query(value = "SELECT count(*) FROM notification_message m "
            + "RIGHT JOIN notification_message_sending_history h ON m.id = h.message_id "
            + "WHERE m.channel_id = :channelId "
            + "and h.create_time > now() - INTERVAL :interval MINUTE "
            + "and m.status = 'SENT_SUCCESSFULLY'",
            nativeQuery = true)
    int countMessageSendingTimesByChannelId(@Param("channelId") Long channelId,
            @Param("interval") Long intervalMinutes);
}
